#include <iostream>
#include <pthread.h>
#include "BlockQueue.hpp"
#include <unistd.h>

void *Product(void *args)
{
    BlockQueueModule::BlockQueue<int> *bq = static_cast<BlockQueueModule::BlockQueue<int> *>(args);
    int data = 1;

    while (true)
    {
        // sleep(2);
        bq->Push(data);
        std::cout << "producter生产数据: " << std::to_string(data) << std::endl;
        data++;
    }

    return nullptr;
}

void *Consum(void *args)
{
    BlockQueueModule::BlockQueue<int> *bq = static_cast<BlockQueueModule::BlockQueue<int> *>(args);
 
    while (true)
    {
        sleep(2);
        int data;
        bq->Pop(data);

        std::cout << "consumer获得数据: " << std::to_string(data) << std::endl;
    }

    return nullptr;
}

int main()
{
    BlockQueueModule::BlockQueue<int> *bq = new BlockQueueModule::BlockQueue<int>;
    pthread_t c1, c2, c3, p1, p2;

    pthread_create(&p1, nullptr, Product, bq);
    pthread_create(&p2, nullptr, Product, bq);
    pthread_create(&c1, nullptr, Consum, bq);
    pthread_create(&c2, nullptr, Consum, bq);
    pthread_create(&c3, nullptr, Consum, bq);

    pthread_join(p1, nullptr);
    pthread_join(p2, nullptr);
    pthread_join(c1, nullptr);
    pthread_join(c2, nullptr);
    pthread_join(c3, nullptr);
    return 0;
}